﻿namespace MyServices
{    
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Web.Api;

    public class WelcomeController : DnnApiController
    {
        [DnnAuthorize]
        [HttpGet]
        public HttpResponseMessage KeepAlive()
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, "True");
            }
            catch (Exception ex)
            {
                Exceptions.LogException(exc: ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [DnnAuthorize]
        [HttpGet]
        public HttpResponseMessage HelloWorld()
        {
            return Request.CreateResponse(HttpStatusCode.OK, UserInfo.Username);
        }

        [DnnAuthorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public HttpResponseMessage GoodbyeWorld(DTOWorldDetails data)
        {
            try
            {
                var helloWorld = "Hello World!";
                if (data.Goodbye)
                {
                    helloWorld = "Good-bye World!";
                }
                return Request.CreateResponse(HttpStatusCode.OK, value: helloWorld);
            }
            catch (Exception ex)
            {
                Exceptions.LogException(exc: ex);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message: ex.Message);
            }
        }
    }
}